
@rendermode InteractiveServer

    <div class="card">
        <div class="card-header">
            <h6>工单标题: @_workOrder.工单标题</h6>
        </div>
        <div class="card-body">
            <div class="row">
                <div class="col-md-6">
                    <p><strong>工单编号：</strong>@_workOrder.工单编号</p>
                    <p><strong>创建人：</strong>@_workOrder.创建人id</p>
                    <p><strong>创建时间：</strong>@_workOrder.创建时间.ToString("yyyy-MM-dd HH:mm:ss")</p>
                    <p><strong>截止时间：</strong>@_workOrder.截止时间?.ToString("yyyy-MM-dd HH:mm:ss")</p>
                </div>
                <div class="col-md-6">
                    <p><strong>当前状态：</strong>
                    @if (_workOrder != null)
                    {
                        <Badge IsPill="true" Color="@GetStatusColor(_workOrder.当前状态)">@_workOrder.当前状态</Badge>
                    }
                    </p>
                    <p><strong>优先级：</strong>
                    
                    @if (_workOrder != null)
                    {
                        <Badge IsPill="true" Color="@GetPriorityColor(_workOrder.优先级)">@_workOrder.优先级</Badge>
                    }
                
                    
                    </p>
                    <p><strong>类型：</strong>@_workOrder.类型</p>
                    <p><strong>最后更新时间：</strong>@_workOrder.更新时间.ToString("yyyy-MM-dd HH:mm:ss")</p>
                </div>
            </div>
            <hr />
            <div class="row">
                <div class="col-12">
                
                    <h5>工单内容：</h5>
                    @foreach (var field in _templateFields)
                    {
                        <div class="col-md-6">
                            <p><strong>@field.字段名称:</strong>
                                @if (_workOrder.扩展字段.TryGetValue(field.字段名称, out var value))
                                {
                                    @value
                                }
                                else
                                {
                                    <span class="text-muted">未填写</span>
                                }
                            </p>
                        </div>
                    }
                </div>
            </div>
      
        @if ( Handlers.Count>0)
        {
                <div class="row">
                        <div class="col-12">
                            <h5>处理人</h5>
                            
                        @foreach (var handler in Handlers)
                            { 
                            <Badge IsPill="true" Color="Color.Success">@handler.用户id</Badge>;
                            }
                        </div>
                    </div>


        }
            
            
            <hr />
            <h5>工单附件</h5>
            @if (_workOrder.工单附件表s.Count > 0)
            {      
                    <Row   ItemsPerRow="ItemsPerRow.Three">
     
                            @foreach (var lsit in _workOrder.工单附件表s.Where(cm=>cm.是否提交==true))
                            {
                          
                            <ImageViewer Url="@lsit.存储路径" ShowPlaceHolder="true" />
              
                            }
                        
               
             
                    </Row>
          
            } 


              <hr />
                @if (_workOrder.工单评论表s.Count > 0)
                {
                    <div class="row">
                        <div class="col-12">
                        
                            @if (_workOrder.工单评论表s.Any(c =>c.评论类型== CommentType.审批评论))
                            {
                                <h6>审批评论</h6>
                                <ul >
                                    @foreach (var comment in _workOrder.工单评论表s.Where(c =>c.评论类型== CommentType.审批评论))
                                    {
                                        <li >
                                            <strong>评论人：</strong>@comment.用户id<br />
                                            <strong>评论时间：</strong>@comment.创建时间.ToString("yyyy-MM-dd HH:mm:ss")<br />
                                            <strong>内容：</strong>@comment.评论内容
                                        </li>
                                    }
                                </ul>
                            }
                            else
                            {
                                <p class="text-muted">暂无审批评论。</p>
                            }

                        
                            @if (_workOrder.工单评论表s.Any(c =>c.评论类型== CommentType.处理评论))
                            {
                                <h6>处理评论</h6>
                                <ul  >
                                    @foreach (var comment in _workOrder.工单评论表s.Where(c =>c.评论类型== CommentType.处理评论))
                                    {
                                        <li  >
                                            <strong>评论人：</strong>@comment.用户id<br />
                                            <strong>评论时间：</strong>@comment.创建时间.ToString("yyyy-MM-dd HH:mm:ss")<br />
                                            <strong>内容：</strong>@comment.评论内容
                                        </li>
                                    }
                                </ul>
                            }
                            else
                            {
                                <p class="text-muted">暂无处理评论。</p>
                            }
                            
                        </div>
                    </div>
                }
                else
                {
                    <p class="text-muted">暂无评论。</p>
                }

        </div>
    </div>
@code
{ 
 
[Parameter]
[Required] 
public required string TicketID { get; set; }
[Inject]
private  IWorkOrderService WorkOrderService { get; set; }
[Inject]
private  MyIdentityContext AppContext   { get; set; }
private 工单主表 _workOrder = new 工单主表();

private List<模板字段表> _templateFields = new();

private List<工单分派记录表> Handlers= new List<工单分派记录表>();
protected override async Task OnInitializedAsync()
{  
await LoadWorkOrderAsync();
}
private async Task LoadWorkOrderAsync()
{
_workOrder = await WorkOrderService.GetWorkOrderByIdAsync(TicketID);

if (_workOrder == null)
{
    return;
}
Handlers =  _workOrder.工单分派记录表s.Where(cm=>cm.是否当前负责人==true).ToList();
if (!AppContext.Roles.Contains("Administrator") &&
    _workOrder.创建人id != AppContext.UserName &&
    !Handlers.Any(cm => cm.用户id == AppContext.UserName) &&
    !AppContext.Roles.Contains("Auditor"))
{
    // 如果当前用户既不是管理员、创建人、处理人，也不是审核员，则显示无权限信息
    _workOrder = null;
    return;
} 
 var template = _workOrder.关联模板;
    if (template != null)
            {
                _templateFields = template.字段定义.OrderBy(f => f.排序序号).ToList();
    }
}
    



public async Task RefreshAsync()
{
    // 子组件的刷新逻辑
    await LoadWorkOrderAsync();
    StateHasChanged(); // 通知 Blazor 重新渲染组件
}
private Color GetStatusColor(WorkOrderStatus status) => status switch
{
    WorkOrderStatus.待处理 => Color.Warning,
    WorkOrderStatus.已分派 => Color.Info,
    WorkOrderStatus.处理中 => Color.Primary,
    WorkOrderStatus.已解决 => Color.Success,
    WorkOrderStatus.已关闭 => Color.Dark,
    WorkOrderStatus.重新打开 => Color.Danger,
    WorkOrderStatus.审核中 => Color.Secondary,
    _ => Color.Light
};
private Color GetPriorityColor(PriorityLevel priority) => priority switch
{
    PriorityLevel.低 => Color.Secondary,
    PriorityLevel.正常 => Color.Success,
    PriorityLevel.高 => Color.Warning,
    PriorityLevel.紧急 => Color.Danger,
    _ => Color.Light
};

}